package cnedu.ncist.paper.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.mysql.jdbc.Statement;

import edu.ncist.wang.hkdf.bussiness.blo.AbstractService;
import edu.ncist.wang.hkdf.dao.database.DBConnectionException;
import edu.ncist.wang.hkdf.dao.database.DataBaseAccess;
import edu.ncist.wang.hkdf.dao.database.DataManipulationException;

public class PaperService extends AbstractService {

	public Map doServiceProcess(Map map) {
		// TODO Auto-generated method stub
		String actionType = (String)map.get("ACTIONTYPE");
		
		if("paper_Emphasis".equals(actionType)){
			return paper_Emphasis(map);
		}
		else if("paper_EmphasisSelect".equals(actionType)){
			return paper_EmphasisSelect(map);
		}
		else if("paper_EmphasisChapter".equals(actionType)){
			return paper_EmphasisChapter(map);
		}
		else if("paper_Hard".equals(actionType)){
			return paper_Hard(map);
		}
		else if("paper_HardSelect".equals(actionType)){
			return paper_HardSelect(map);
		}
		else if("paper_ItemScore".equals(actionType)){
			return paper_ItemScore(map);
		}
		else if("paper_ItemScoreInit".equals(actionType)){
			return paper_ItemScoreInit(map);
		}
		else if("paper_ItemScoreSelect".equals(actionType)){
			return paper_ItemScoreSelect(map);
		}
		else if("paper_Submit".equals(actionType)){
			return paper_Submit(map);
		}
		else if("paper_SubmitSelect".equals(actionType)){
			return paper_SubmitSelect(map);
		}
		else{
			ActionTypeError();
		}

		return null;
	}

	public Map<String, Object> paper_Emphasis(Map paraMap){
		
		
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<Map<String, String>> paperList = null;
		List<Map<String,String>> paperList1 = null;
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();
		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		   
	   	String sql = "SELECT item_id,course_name,"+
	   			"case  "+
	   			"when refer_chapter like '%第一章%' then '第一章' "+
	   			"when refer_chapter like '%第二章%' then '第二章' "+
	   			"when refer_chapter like '%第三章%' then '第三章'   "+
	   			"when refer_chapter like '%第四章%' then '第四章'   "+
	   			"when refer_chapter like '%第五章%' then '第五章' "+
	   				"when refer_chapter like '%第六章%' then '第六章' "+ 
	   		   			"else '第七章'  "+
	   		   			"end as 'chapter', "+
	   		   			"case   "+
	   		   			"when item_id=1 then '第一题'   "+
	   							"when item_id=3 then '第三题' "+
	   							"when item_id=4 then '第四题' "+
	   		   			"when item_id=2 then '第二题'   "+
	   					"when item_id=5 then '第五题' "+
	   					"when item_id=6 then '第六题'  "+
	   					"else '第七题'  "+
	   					"end as 'item',"+
	   			"case "+
	   			"when item_type=0 then '主观题'"+
	   			"else '客观题'"+
	   			"end as 'item_type',"
	   			+ "count(item_id)/(select count(item_id) from testpaper where chose_course_id='"+courseId+"' and course_id='"+classId+"')as 'item_rate'  "+
	   			"from testpaper,course where testpaper.chose_course_id=course.course_id and chose_course_id='"+courseId+"' and testpaper.course_id='"+classId+"' GROUP BY item_id";

	   	System.out.println(sql);
	   
	   	
	   	try{
			paperList = dba.getMultiRecord(sql);
			dba.close();
		}
		catch(DataManipulationException e){
			System.out.println("get Emphasis info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		
		resultMap.put("PAPERINFO", paperList);
		resultMap.put("CONDITIONMAP", map);
		
		return resultMap;
		
	}
	
	public Map<String, Object> paper_EmphasisSelect(Map paraMap){
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		String item_type = map.get("ITEM_TYPE").toString();
		String testChapt = map.get("TEST_CHAPT").toString();

		Map<String, Object> resultMap = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();
		List<Map<String, String>> paperList = null;
		
		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		   
	   	String sql = null;
	   	if(item_type.equals("")&&testChapt.equals("")){
	   		sql = "SELECT item_id,course_name,"+
		   			"case  "+
		   			"when refer_chapter like '%第一章%' then '第一章' "+
		   			"when refer_chapter like '%第二章%' then '第二章' "+
		   			"when refer_chapter like '%第三章%' then '第三章'   "+
		   			"when refer_chapter like '%第四章%' then '第四章'   "+
		   			"when refer_chapter like '%第五章%' then '第五章' "+
		   				"when refer_chapter like '%第六章%' then '第六章' "+ 
		   		   			"else '第七章'  "+
		   		   			"end as 'chapter', "+
		   		   			"case   "+
		   		   			"when item_id=1 then '第一题'   "+
		   							"when item_id=3 then '第三题' "+
		   							"when item_id=4 then '第四题' "+
		   		   			"when item_id=2 then '第二题'   "+
		   					"when item_id=5 then '第五题' "+
		   					"when item_id=6 then '第六题'  "+
		   					"else '第七题'  "+
		   					"end as 'item',"+
		   			"case "+
		   			"when item_type=0 then '主观题'"+
		   			"else '客观题'"+
		   			"end as 'item_type',"
		   			+ "count(item_id)/(select count(item_id) from testpaper where chose_course_id='"+courseId+"' and course_id='"+classId+"')as 'item_rate'  "+
		   			"from testpaper,course where testpaper.chose_course_id=course.course_id and chose_course_id='"+courseId+"' and testpaper.course_id='"+classId+"' GROUP BY item_id";
}
	   	else if(!item_type.equals("")&&testChapt.equals("")){
	   		sql = "SELECT item_id,course_name,"+
		   			"case  "+
		   			"when refer_chapter like '%第一章%' then '第一章' "+
		   			"when refer_chapter like '%第二章%' then '第二章' "+
		   			"when refer_chapter like '%第三章%' then '第三章'   "+
		   			"when refer_chapter like '%第四章%' then '第四章'   "+
		   			"when refer_chapter like '%第五章%' then '第五章' "+
		   				"when refer_chapter like '%第六章%' then '第六章' "+ 
		   		   			"else '第七章'  "+
		   		   			"end as 'chapter', "+
		   		   			"case   "+
		   		   			"when item_id=1 then '第一题'   "+
		   							"when item_id=3 then '第三题' "+
		   							"when item_id=4 then '第四题' "+
		   		   			"when item_id=2 then '第二题'   "+
		   					"when item_id=5 then '第五题' "+
		   					"when item_id=6 then '第六题'  "+
		   					"else '第七题'  "+
		   					"end as 'item',"+
		   			"case "+
		   			"when item_type=0 then '客观题'"+
		   			"else '主观题'"+
		   			"end as 'item_type',"
		   			+ "count(item_id)/(select count(item_id) from testpaper where chose_course_id='"+courseId+"' and course_id='"+classId+"')as 'item_rate'  "+
		   			"from testpaper,course where testpaper.chose_course_id=course.course_id and chose_course_id='"+courseId+"' and testpaper.course_id='"+classId+"'  "+
	   					"and item_type='"+item_type+"' GROUP BY refer_chapter";
	   	}
	   	else if(item_type.equals("")&&!testChapt.equals("")){
	   		sql ="SELECT item_id,course_name,"+
		   			"case  "+
		   			"when refer_chapter like '%第一章%' then '第一章' "+
		   			"when refer_chapter like '%第二章%' then '第二章' "+
		   			"when refer_chapter like '%第三章%' then '第三章'   "+
		   			"when refer_chapter like '%第四章%' then '第四章'   "+
		   			"when refer_chapter like '%第五章%' then '第五章' "+
		   				"when refer_chapter like '%第六章%' then '第六章' "+ 
		   		   			"else '第七章'  "+
		   		   			"end as 'chapter', "+
		   		   			"case   "+
		   		   			"when item_id=1 then '第一题'   "+
		   							"when item_id=3 then '第三题' "+
		   							"when item_id=4 then '第四题' "+
		   		   			"when item_id=2 then '第二题'   "+
		   					"when item_id=5 then '第五题' "+
		   					"when item_id=6 then '第六题'  "+
		   					"else '第七题'  "+
		   					"end as 'item',"+
		   			"case "+
		   			"when item_type=0 then '主观题'"+
		   			"else '客观题'"+
		   			"end as 'item_type',"
		   			+ "count(item_id)/(select count(item_id) from testpaper where chose_course_id='"+courseId+"' and course_id='"+classId+"')as 'item_rate'  "+
		   			"from testpaper,course where testpaper.chose_course_id=course.course_id and chose_course_id='"+courseId+"' and testpaper.course_id='"+classId+"'  "

	   					+"and refer_chapter like '%"+testChapt+"%' GROUP BY item_id ";
	   	}
	   	else{
	   		sql ="SELECT item_id,course_name,"+
		   			"case  "+
		   			"when refer_chapter like '%第一章%' then '第一章' "+
		   			"when refer_chapter like '%第二章%' then '第二章' "+
		   			"when refer_chapter like '%第三章%' then '第三章'   "+
		   			"when refer_chapter like '%第四章%' then '第四章'   "+
		   			"when refer_chapter like '%第五章%' then '第五章' "+
		   				"when refer_chapter like '%第六章%' then '第六章' "+ 
		   		   			"else '第七章'  "+
		   		   			"end as 'chapter', "+
		   		   			"case   "+
		   		   			"when item_id=1 then '第一题'   "+
		   							"when item_id=3 then '第三题' "+
		   							"when item_id=4 then '第四题' "+
		   		   			"when item_id=2 then '第二题'   "+
		   					"when item_id=5 then '第五题' "+
		   					"when item_id=6 then '第六题'  "+
		   					"else '第七题'  "+
		   					"end as 'item',"+
		   			"case "+
		   			"when item_type=0 then '主观题'"+
		   			"else '客观题'"+
		   			"end as 'item_type',"
		   			+ "count(item_id)/(select count(item_id) from testpaper where chose_course_id='"+courseId+"' and course_id='"+classId+"')as 'item_rate'  "+
		   			"from testpaper,course where testpaper.chose_course_id=course.course_id and chose_course_id='"+courseId+"' and testpaper.course_id='"+classId+"'  "

	   					+"and refer_chapter like '%"+testChapt+"%' and item_type='"+item_type+"' and testpaper.course_id='"+classId+"' GROUP BY refer_chapter";
	   		
	   	}
	   	try{
	   		paperList = dba.getMultiRecord(sql);
			dba.close();
		}
		catch(DataManipulationException e){
			System.out.println("get hard info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		
		resultMap.put("PAPERINFO", paperList);
		resultMap.put("CONDITIONMAP", map);
		
		return resultMap;
	}
		//没有用
	public Map<String, Object> paper_EmphasisChapter(Map paraMap){
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		String chapter = map.get("CHAPTER").toString();

		Map<String, Object> resultMap = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		List<Map<String, String>> paperList = null;
		
		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		   
	   	String sql = null;
	   		sql = "SELECT id from testpaper where refer_chapter like '%"+chapter+"%' "+
	 	   		"and chose_course_id='"+courseId+"'  ";	
	   	
	   	try{
	   		paperList = dba.getMultiRecord(sql);
			dba.close();
		}
		catch(DataManipulationException e){
			System.out.println("get hard info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		
		resultMap.put("PAPERINFO", paperList);
		resultMap.put("CONDITIONMAP", map);
		
		return resultMap;
	}
	public Map<String, Object> paper_Hard(Map paraMap){
		
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");

		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<Map<String, String>> paperList = null;
		List<Map<String,String>> paperList1=null;
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();
		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		DataBaseAccess dba1 = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
	   	String sql = "select case  "+ 
		   			"when item_id=1 then '第一题'  "+
		   			"when item_id=2 then '第二题'  "+
		   			"when item_id=3 then '第三题'  "+
		   			"when item_id=4 then '第四题'  "+
		   			"when item_id=5 then '第五题'  "+
		   			"when item_id=6 then '第六题'  "+
		   			"else  '第七题'  "+
		   			"end as 'item',item_content,refer_chapter,"+
		   			"CASE  "+
		   			"when item_1=1 then round(sum(score)/(count(student_id)*20) ,2) "+
			   			"else round(sum(score)/(count(student_id)*40) ,2) "+
						"end as 'cort_rate',"+
						"case  "+
						"when item_1=1 then ROUND(1-sum(score)/(count(student_id)*20),2)  "+
						"else round(1-sum(score)/(count(student_id)*40),2)   "+
						"end as 'hard_rate' , "+
						"case  "+ 
			   			"when item_type=0 then '客观题'  "+
			   			"else '主观题'   "+ 
			   			"end as 'item_type'  "+ 
			   			"from courseinfo,testpaper where testpaper.item_id=courseinfo.item_1 and testpaper.course_id='"+classId+"' and courseinfo.chose_course_id='"+courseId+"' group by item_id";
	   	/*String sql1 = "select count(student_id),"+
						"case "+
						"when item_1=1 then sum(score)/(count(student_id)*20)  "+
						"else sum(score)/(count(student_id)*40) "+
						"end as 'cort_rate', "+
						"case  "+
						"when item_1=1 then 1-sum(score)/(count(student_id)*20) "+
						"else 1-sum(score)/(count(student_id)*40) " +
						"end as 'hard_rate' "+
						"from courseinfo where chose_course_id='"+courseId+"' and course_class_id='"+classId+"' group by item_1";
	   	*/
		try{
			paperList = dba.getMultiRecord(sql);
			//paperList1 = dba1.getMultiRecord(sql1);
			dba.close();
			dba1.close();
		}
		catch(DataManipulationException e){
			System.out.println("get hard info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		
		resultMap.put("PAPERINFO", paperList);
		resultMap.put("PAPERINFO1",paperList1);
		
		resultMap.put("CONDITIONMAP", map);
		
		return resultMap;
		
	}
	public Map<String, Object> paper_HardSelect(Map paraMap){
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		String testId = map.get("TEST_ID").toString();
		String testChapt = map.get("TEST_CHAPT").toString();

		Map<String, Object> resultMap = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();
		List<Map<String, String>> paperList = null;
		
		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		   
	   	String sql = null;
	   	if(testId.equals("")&&testChapt.equals("")){
	   		sql = "select case  "+ 
		   			"when item_id=1 then '第一题'  "+
		   			"when item_id=2 then '第二题'  "+
		   			"when item_id=3 then '第三题'  "+
		   			"when item_id=4 then '第四题'  "+
		   			"when item_id=5 then '第五题'  "+
		   			"when item_id=6 then '第六题'  "+
		   			"else  '第七题'  "+
		   			"end as 'item',item_content,refer_chapter,"+
		   			"CASE  "+
		   			"when item_1=1 then round(sum(score)/(count(student_id)*20) ,2) "+
			   			"else round(sum(score)/(count(student_id)*40) ,2) "+
						"end as 'cort_rate',"+
						"case  "+
						"when item_1=1 then ROUND(1-sum(score)/(count(student_id)*20),2)  "+
						"else round(1-sum(score)/(count(student_id)*40),2)   "+
						"end as 'hard_rate' , "+
						"case  "+ 
			   			"when item_type=0 then '客观题'  "+
			   			"else '主观题'   "+ 
			   			"end as 'item_type'  "+ 
			   			"from courseinfo,testpaper where testpaper.item_id=courseinfo.item_1 and testpaper.course_id='"+classId+"' and courseinfo.chose_course_id='"+courseId+"' group by item_id";
	   	}
	   	else if(!testId.equals("")&&testChapt.equals("")){
	   		sql = "select case  "+ 
	   				"when item_id=1 then '第一题'  "+
		   			"when item_id=2 then '第二题'  "+
		   			"when item_id=3 then '第三题'  "+
		   			"when item_id=4 then '第四题'  "+
		   			"when item_id=5 then '第五题'  "+
		   			"when item_id=6 then '第六题'  "+
		   			"else  '第七题'  "+
		   			"end as 'item',item_content,refer_chapter,"+
		   			"CASE  "+
		   			"when item_1=1 then round(sum(score)/(count(student_id)*20) ,2) "+
			   			"else round(sum(score)/(count(student_id)*40) ,2) "+
						"end as 'cort_rate',"+
						"case  "+
						"when item_1=1 then ROUND(1-sum(score)/(count(student_id)*20),2)  "+
						"else round(1-sum(score)/(count(student_id)*40),2)   "+
						"end as 'hard_rate' , "+
						"case  "+ 
			   			"when item_type=0 then '客观题'  "+
			   			"else '主观题'   "+ 
			   			"end as 'item_type'  "+ 
			   			"from courseinfo,testpaper where testpaper.item_id=courseinfo.item_1 and testpaper.course_id='"+classId+"' and courseinfo.chose_course_id='"+courseId+"'  "+
	   					"and item_id='"+testId+"' group by item_id";
	   	}
	   	else if(testId.equals("")&&!testChapt.equals("")){
	   		sql =  "select case  "+ 
	   				"when item_id=1 then '第一题'  "+
		   			"when item_id=2 then '第二题'  "+
		   			"when item_id=3 then '第三题'  "+
		   			"when item_id=4 then '第四题'  "+
		   			"when item_id=5 then '第五题'  "+
		   			"when item_id=6 then '第六题'  "+
		   			"else  '第七题'  "+
		   			"end as 'item',item_content,refer_chapter,"+
		   			"CASE  "+
		   			"when item_1=1 then round(sum(score)/(count(student_id)*20) ,2) "+
			   			"else round(sum(score)/(count(student_id)*40) ,2) "+
						"end as 'cort_rate',"+
						"case  "+
						"when item_1=1 then ROUND(1-sum(score)/(count(student_id)*20),2)  "+
						"else round(1-sum(score)/(count(student_id)*40),2)   "+
						"end as 'hard_rate' , "+
						"case  "+ 
			   			"when item_type=0 then '客观题'  "+
			   			"else '主观题'   "+ 
			   			"end as 'item_type'  "+ 
			   			"from courseinfo,testpaper where testpaper.item_id=courseinfo.item_1 and testpaper.course_id='"+classId+"' and courseinfo.chose_course_id='"+courseId+"'  "+
	   					"and refer_chapter like '%"+testChapt+"%' group by item_id";
	   					 
	   	}
	   	else{
	   		sql = "select case  "+ 
	   				"when item_id=1 then '第一题'  "+
		   			"when item_id=2 then '第二题'  "+
		   			"when item_id=3 then '第三题'  "+
		   			"when item_id=4 then '第四题'  "+
		   			"when item_id=5 then '第五题'  "+
		   			"when item_id=6 then '第六题'  "+
		   			"else  '第七题'  "+
		   			"end as 'item',item_content,refer_chapter,"+
		   			"CASE  "+
		   			"when item_1=1 then round(sum(score)/(count(student_id)*20) ,2) "+
			   			"else round(sum(score)/(count(student_id)*40) ,2) "+
						"end as 'cort_rate',"+
						"case  "+
						"when item_1=1 then ROUND(1-sum(score)/(count(student_id)*20),2)  "+
						"else round(1-sum(score)/(count(student_id)*40),2)   "+
						"end as 'hard_rate' , "+
						"case  "+ 
			   			"when item_type=0 then '客观题'  "+
			   			"else '主观题'   "+ 
			   			"end as 'item_type'  "+ 
			   			"from courseinfo,testpaper where testpaper.item_id=courseinfo.item_1 and testpaper.course_id='"+classId+"' and courseinfo.chose_course_id='"+courseId+"'  "+
	   					"and refer_chapter like '%"+testChapt+"%' and item_id='"+testId+"' group by item_1";
	   		
	   	}
	   	try{
	   		paperList = dba.getMultiRecord(sql);
			dba.close();
		}
		catch(DataManipulationException e){
			System.out.println("get hard info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		
		resultMap.put("PAPERINFO", paperList);
		resultMap.put("CONDITIONMAP", map);
		
		return resultMap;
	}

	public Map<String, Object> paper_ItemScore(Map paraMap){
		
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Map<String, String>> paperList = null;
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();

		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
	   	String sql = "select item_1,course_name,round(avg(score),2)as avg from courseinfo,student,course where "
	   			+ "student.student_id=courseinfo.student_id and course.course_id=courseinfo.chose_course_id and   "+
	   			" chose_course_id='"+courseId+"' and course_class_id='"+classId+"' group by item_1";
		try{
			paperList = dba.getMultiRecord(sql);
			dba.close();
		}
		catch(DataManipulationException e){
			System.out.println("get paper info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		
		resultMap.put("PAPERINFO", paperList);
		resultMap.put("CONDITIONMAP", map);
		
		return resultMap;
	}
	public Map<String,Object> paper_ItemScoreInit(Map paraMap){
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");

		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<Map<String, String>> paperList = null;
		
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();

		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		
	   	String sql = "select courseinfo.student_id,student_name,CASE "+
	   			"when item_1=1 then '第一题'  "+
	   			"when item_1=2 then '第二题'  "+
	   			"when item_1=3 then '第三题'  "+
	   			"when item_1=4 then '第四题'  "+
	   			"when item_1=5 then '第五题'  "+
	   			"when item_1=6 then '第六题'  "+
	   			"else  '第七题'  "+
	   				"end as 'item',score  "+
	   				"from courseinfo,student where   "
	   				+ "student.student_id=courseinfo.student_id and course_class_id='"+classId+"' and chose_course_id='"+courseId+"'";
	   			System.out.println(sql);
	   			
	   
	   			try{
			paperList = dba.getMultiRecord(sql);
			
					
			dba.close();
			
		}
		catch(DataManipulationException e){
			System.out.println("get test info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		
		resultMap.put("PAPERINFO", paperList);
		//resultMap.put("PAPERINFO1",paperList1);
		//resultMap.put("PAPERINFO2", paperList2);
		System.out.println("******"+paperList);
		resultMap.put("CONDITIONMAP", map);
		
		return resultMap;
	}
	
	public Map<String, Object> paper_ItemScoreSelect(Map paraMap){
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		
		String name = map.get("STU_NAME").toString();
		String stuid = map.get("STU_ID").toString();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<Map<String, String>> paperList = null;
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();

		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		String sql=null;
		//瀛﹀彿鍜屽鍚嶉兘涓虹┖
		if ((stuid.equals(null) || stuid.equals("")) && 
				(name.equals(null) || name.equals(""))){
			sql = "select courseinfo.student_id,student_name,CASE "+
					"when item_1=1 then '第一题'  "+
		   			"when item_1=2 then '第二题'  "+
		   			"when item_1=3 then '第三题'  "+
		   			"when item_1=4 then '第四题'  "+
		   			"when item_1=5 then '第五题'  "+
		   			"when item_1=6 then '第六题'  "+
		   			"else  '第七题'  "+
	   				"end as 'item',score  "+
	   				"from courseinfo,student where   "
	   				+ "student.student_id=courseinfo.student_id and course_class_id='"+classId+"' and chose_course_id='"+courseId+"'";
		}
		//瀛﹀彿涓虹┖浣嗗鍚嶄笉涓虹┖
		else if((stuid.equals(null) || stuid.equals("")) &&!name.equals(null) && !name.equals(""))
		{
			
			sql = "select courseinfo.student_id,student_name,CASE "+
					"when item_1=1 then '第一题'  "+
		   			"when item_1=2 then '第二题'  "+
		   			"when item_1=3 then '第三题'  "+
		   			"when item_1=4 then '第四题'  "+
		   			"when item_1=5 then '第五题'  "+
		   			"when item_1=6 then '第六题'  "+
		   			"else  '第七题'  "+
	   				"end as 'item',score  "+
	   				"from courseinfo,student where   "
	   				+ "student.student_id=courseinfo.student_id and course_class_id='"+classId+"' and chose_course_id='"+courseId+"'and student.student_name='"+name+"' ";
			
		}
		//濮撳悕涓虹┖瀛﹀彿涓嶄负绌�
		else if(!stuid.equals(null) && !stuid.equals("")&&(name.equals(null) || name.equals("")))
		{
			sql = "select courseinfo.student_id,student_name,CASE "+
					"when item_1=1 then '第一题'  "+
		   			"when item_1=2 then '第二题'  "+
		   			"when item_1=3 then '第三题'  "+
		   			"when item_1=4 then '第四题'  "+
		   			"when item_1=5 then '第五题'  "+
		   			"when item_1=6 then '第六题'  "+
		   			"else  '第七题'  "+
	   				"end as 'item',score  "+
	   				"from courseinfo,student where   "
	   				+ "student.student_id=courseinfo.student_id and course_class_id='"+classId+"' and chose_course_id='"+courseId+"' and student.student_id='"+stuid+"' ";
		}
		//濮撳悕鍜屽鍙烽兘涓嶄负绌�
		else if(!stuid.equals(null) && !stuid.equals("")&&!name.equals(null) && !name.equals("")){
			
			sql = "select courseinfo.student_id,student_name,CASE "+
					"when item_1=1 then '第一题'  "+
		   			"when item_1=2 then '第二题'  "+
		   			"when item_1=3 then '第三题'  "+
		   			"when item_1=4 then '第四题'  "+
		   			"when item_1=5 then '第五题'  "+
		   			"when item_1=6 then '第六题'  "+
		   			"else  '第七题'  "+
	   				"end as 'item',score  "+
	   				"from courseinfo,student where   "
	   				+ "student.student_id=courseinfo.student_id and course_class_id='"+classId+"' and chose_course_id='"+courseId+"' "+
	   				  " and student.student_id = '"+stuid+"' and student.student_name = '"+name+"' ";
			
		}
		 
		try{
			paperList = dba.getMultiRecord(sql);
			dba.close();
		}
		catch(DataManipulationException e){
			System.out.println("get test info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		resultMap.put("PAPERINFO", paperList);
		resultMap.put("CONDITIONMAP", map);
		System.out.println(paperList+"0000000000");
		
		return resultMap;
		
	}
	public Map<String, Object> paper_Submit(Map paraMap){
		
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		

		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<Map<String,String>> paperList1 = null;
		List<Map<String, String>> paperList = null;
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();

		DataBaseAccess dba1 = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		
		String sql1="select test_submit.course_id,course_name,sum(submit) as submit,COUNT(last_submit_time)as al_submit,sum(submit)-count(last_submit_time) as undo_submit  "+  
				" from test_submit ,test_score,course where test_submit.student_id = test_score.student_id and test_submit.course_id='"+courseId+"' "+
				"and course_class_id='"+classId+"' and course.course_id=test_submit.course_id";
		
		String sql="select test_submit.student_id,student_name,"+
					"CASE "+
					"when last_submit_time is null  then '否'  "+
					"ELSE '是' "+
					"end AS 'is_submit',"+
					"last_submit_time,avg_consume_memory,avg_run_time from test_submit,test_score,student where "+
					"test_score.student_id=test_submit.student_id and student.student_id=test_submit.student_id and   "+
					"test_submit.course_id='"+courseId+"' "+
					"and course_class_id='"+classId+"'";
		
		
		try{
			paperList1 = dba1.getMultiRecord(sql1);
			paperList = dba.getMultiRecord(sql);
			dba1.close();
			dba.close();
			
		}
		catch(DataManipulationException e){
			System.out.println("get test info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList||null==paperList1){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		resultMap.put("PAPERINFO1", paperList1);
		resultMap.put("PAPERINFO", paperList);
		System.out.println("paperList="+resultMap);
		resultMap.put("CONDITIONMAP", map);
		return resultMap;
	}
		
	
public Map<String, Object> paper_SubmitSelect(Map paraMap){
		
		HttpServletRequest request = (HttpServletRequest)paraMap.get("REQUEST");
		Map map = (Map)paraMap.get("CONDITIONMAP");
		
		String testId = (String) map.get("TEST_ID");
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		List<Map<String,String>> paperList1 = null;
		List<Map<String, String>> paperList = null;
		HttpSession session = request.getSession();
		String courseId=session.getAttribute("COURSEID").toString();
		String classId=session.getAttribute("CLASSID").toString();

		DataBaseAccess dba1 = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		DataBaseAccess dba = (DataBaseAccess)request.getAttribute("AUTHDATACONNECTION");
		String sql = null;
		String sql1 = null;
		System.out.println("testID=="+testId);
		if(testId.equals("")){
		 sql1="select test_submit.course_id,course_name,sum(submit) as submit,COUNT(last_submit_time)as al_submit,sum(submit)-count(last_submit_time) as undo_submit  "+  
					" from test_submit ,test_score,course where test_submit.student_id = test_score.student_id and test_submit.course_id='"+courseId+"' "+
					"and course_class_id='"+classId+"' and course.course_id=test_submit.course_id";
		
		 sql="select test_submit.student_id,student_name,"+
					"CASE "+
					"when last_submit_time is null  then '否'  "+
					"ELSE '是' "+
					"end AS 'is_submit',"+
					"last_submit_time,avg_consume_memory,avg_run_time from test_submit,test_score,student where "+
					"test_score.student_id=test_submit.student_id and student.student_id=test_submit.student_id and   "+
					"test_submit.course_id='"+courseId+"' "+
					"and course_class_id='"+classId+"'";
		}else {
			sql1="select test_submit.course_id,course_name,sum(submit) as submit,COUNT(last_submit_time)as al_submit,sum(submit)-count(last_submit_time) as undo_submit  "+  
					" from test_submit ,test_score,course where test_submit.student_id = test_score.student_id and test_submit.course_id='"+courseId+"' "+
					"and course_class_id='"+classId+"' and course.course_id=test_submit.course_id and test_submit.id='"+testId+"' ";
			
			 sql="select test_submit.student_id,student_name,"+
						"CASE "+
						"when last_submit_time is null  then '否'  "+
						"ELSE '是' "+
						"end AS 'is_submit',"+
						"last_submit_time,avg_consume_memory,avg_run_time from test_submit,test_score,student where "+
						"test_score.student_id=test_submit.student_id and student.student_id=test_submit.student_id and   "+
						"test_submit.course_id='"+courseId+"' "+
						"and course_class_id='"+classId+"' "+
							 "and test_submit.id='"+testId+"'";
		}
		
		System.out.println("sql--"+sql);
		System.out.println("sql1--"+sql1);
		 try{
			paperList1 = dba1.getMultiRecord(sql1);
			paperList = dba.getMultiRecord(sql);
			dba1.close();
			dba.close();
			
		}
		catch(DataManipulationException e){
			System.out.println("get test info access database error!");
			e.printStackTrace();
		}
		catch(DBConnectionException e){
			System.out.println("database close error!");
			e.printStackTrace();
		}
		
		if(null == paperList||null==paperList1){
			resultMap.put("RESULT", "fail");
		}
		else{
			resultMap.put("RESULT", "success");
		}
		resultMap.put("PAPERINFO1", paperList1);
		resultMap.put("PAPERINFO", paperList);
		System.out.println("sql="+sql1);
		System.out.println("paperList="+resultMap);
				resultMap.put("TESTID", testId);
		return resultMap;
	}
	}
